package com.qinglei.recoup.system.task;

import com.qinglei.recoup.common.domain.CommonConstant;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.service.RedisService;
import com.qinglei.recoup.system.service.BedHealthBreathDailyDataService;
import com.qinglei.recoup.system.service.BedHealthDailyStatisticsService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Set;
import java.util.UUID;

/**
 * 处理健康数据的定时任务类
 */
@Slf4j
@Component
public class HealthDataTask {
    @Autowired
    private RedisService redisService;

    @Autowired
    private BedHealthDailyStatisticsService bedHealthDailyStatisticsService;

    @Autowired
    private BedHealthBreathDailyDataService bedHealthBreathDailyDataService;

    @Value("${Health.v3BreathTimeSsecond}")
    private Integer v3BreathTimeSsecond;

    @Value("${Health.v3HeartTimeSsecond}")
    private Integer v3HeartTimeSsecond;

    @Value("${Health.v4BreathTimeSsecond}")
    private Integer v4BreathTimeSsecond;

    @Value("${Health.onOrOffV3}")
    private boolean onOrOffV3;

    @Value("${Health.onOrOffV4}")
    private boolean onOrOffV4;

    public void importData() {
        log.info("importData start");
        String code = UUID.randomUUID().toString();
        String processing = null;
        LocalDate yesterday = LocalDate.now().minusDays(1);
        String key = CommonConstant.IMPORT_HEALTH_DATA_LOCK_PREFIX + yesterday.format(DateTimeFormatter.BASIC_ISO_DATE);
        try {
            processing = redisService.get(key);
            if (processing != null) {
                log.info("importData processing exit");
                return;
            }
            redisService.set(key, code, CommonConstant.IMPORT_HEALTH_DATA_LOCK_TIMEOUT);
            processing = code;

            // 处理数据导入
            bedHealthDailyStatisticsService.importDataFromOss(yesterday);
        } catch (Exception e) {
            log.error("importData error", e);
        } finally {
            if (processing != null && processing.equals(code)) {
                try {
                    redisService.del(key);
                } catch (RedisConnectException e) {
                    log.error("importData error", e);
                }
            }
        }
    }

    public void importDataV4() {
        log.info("importDataV4 start");
        String code = UUID.randomUUID().toString();
        String processing = null;
        LocalDate yesterday = LocalDate.now().minusDays(1);
        String key = CommonConstant.IMPORT_HEALTH_DATA_LOCK_PREFIXV4 + yesterday.format(DateTimeFormatter.BASIC_ISO_DATE);
        try {
            processing = redisService.get(key);
            if (processing != null) {
                log.info("importDataV4 processing exit");
                return;
            }
            redisService.set(key, code, CommonConstant.IMPORT_HEALTH_DATA_LOCK_TIMEOUT);
            processing = code;

            // 处理数据导入
            bedHealthDailyStatisticsService.importDataFromOssV4(yesterday);
        } catch (Exception e) {
            log.error("importDataV4 error", e);
        } finally {
            if (processing != null && processing.equals(code)) {
                try {
                    redisService.del(key);
                } catch (RedisConnectException e) {
                    log.error("importDataV4 error", e);
                }
            }
        }
    }

}
